using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;
using NetProtocol.POD;

namespace IQIGame.Onigao.Logic
{
    public class LevelHero : BaseLevelFunctionHost
    {
        public long id => heroData.id;
        public LevelPlayer player { get; }
        public LogicHeroData heroData { get; }
        public override LevelRegion region => player.region;

        public bool isDead => hp == 0;

        public float buffSpeedRatio => _buffManager == null ? 1f : _buffManager.CalcSpeedRatio();

        public int hp
        {
            get
            {
                return heroData.attrs[(int)EAttributeType.Hp];
            }
            set
            {
                heroData.attrs[(int)EAttributeType.Hp] = value;
            }
        }
        public int sp
        {
            get
            {
                return heroData.attrs[(int)EAttributeType.SP];
            }
            set
            {
                heroData.attrs[(int)EAttributeType.SP] = value;
            }
        }

        public LevelHero(LevelPlayer player, HeroPOD heroPOD)
        {
            this.player = player;
            this.heroData = new LogicHeroData(heroPOD);
            ResetLevelAttr();
        }

        public LevelHero(LevelPlayer player, LogicHeroData heroData)
        {
            this.player = player;
            this.heroData = heroData;
            ResetLevelAttr();
        }

        /// <summary>
        /// 重置关卡特有的属性
        /// </summary>
        public void ResetLevelAttr()
        {
            hp = heroData.attrs[(int)EAttributeType.MaxHp];
            sp = heroData.attrs[(int)EAttributeType.InitSP];
        }

        public void SetHP(int hp)
        {
            this.hp = hp;
        }

        public void SetSP(int sp)
        {
            this.sp = sp;
        }

        public void AddHp(int value, bool isPercent)
        {
            if (isDead) //已经死亡，无法再修改HP，只能通过复活重置相关属性
            {
                return;
            }
            int hp = this.hp;
            if (isPercent)
            {
                value = (int)((float)hp * value / 100);
            }
            hp += value;
            if (hp < 0)
            {
                hp = 0;
            }
            else
            {
                int maxHp = heroData.attrs[(int)EAttributeType.MaxHp];
                if (hp > maxHp)
                {
                    hp = maxHp;
                }
            }
            this.hp = hp;
        }

        public override IPOD GeneratePOD()
        {
            LevelHeroPOD pod = new LevelHeroPOD();
            pod.id = heroData.id;
            pod.cid = heroData.cid;
            pod.skinCid = heroData.skinCid;
            pod.awakenLv = heroData.awakenLevel;
            pod.level = heroData.level;
            pod.exp = heroData.exp;
            pod.star = heroData.star;
            pod.hp = this.hp;
            pod.sp = this.sp;
            pod.maxHp = heroData.attrs[(int)EAttributeType.MaxHp];
            pod.maxSp = heroData.attrs[(int)EAttributeType.MaxSP];
            pod.unsafe_buffs = _buffManager?.GenerateBuffPODs();
            return pod;
        }

        public void OnDead()
        {
            if (_buffManager != null)
            {
                _buffManager.RemoveAll();
            }
        }

        public override void OnReset()
        {
            base.OnReset();
        }
    }
}